//
//  WPBingYLVC.m
//  wobingwoyi
//
//  Created by WGP on 2016/12/20.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPBingYLVC.h"
#import "WPAfterBingZFBVC.h"

@interface WPBingYLVC ()

@property(nonatomic,strong)UILabel *tipLabel;
@property(nonatomic,strong)UIView *bgView;

@property(nonatomic,strong)UITextField *cardField;
@property(nonatomic,strong)UITextField *nameField;
@property(nonatomic,strong)UITextField *bankTypeField;

@property(nonatomic,strong)UIButton *bingBtn;

@end

@implementation WPBingYLVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = GlobalBgColor;
    self.titleLabel.text = @"兑换收益";
    
    [self setupUI];
}

-(void)setupUI
{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"YL"]];
    [self.view addSubview:imageView];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.navBarView.mas_bottom).offset(30);
        make.centerX.equalTo(self.navBarView);
    }];
    
    UILabel *tipLabel = [UILabel new];
    self.tipLabel = tipLabel;
    tipLabel.text = @"绑定银行卡成为您的提现账户";
    tipLabel.textColor = RGB_SAME(153);
    tipLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:tipLabel];
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(imageView.mas_bottom).offset(26);
    }];
    
    [self bgView];
}

-(UIView *)bgView
{
    if (_bgView == nil)
    {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_bgView];
        
        [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.tipLabel.mas_bottom).offset(26);
            make.left.right.equalTo(self.view);
            make.height.mas_equalTo(132);
        }];
        
        UILabel *accountLabel = [UILabel new];
        UILabel *nameLabel = [UILabel new];
        UILabel *bangTypeLabel = [UILabel new];
        accountLabel.font = [UIFont systemFontOfSize:15];
        nameLabel.font = [UIFont systemFontOfSize:15];
        bangTypeLabel.font = [UIFont systemFontOfSize:15];
        accountLabel.textColor = GLobalFontColor;
        nameLabel.textColor = GLobalFontColor;
        bangTypeLabel.textColor = GLobalFontColor;
        accountLabel.text = @"银行卡号";
        nameLabel.text = @"真实姓名";
        bangTypeLabel.text = @"银行类型";
        [_bgView addSubview:accountLabel];
        [_bgView addSubview:nameLabel];
        [_bgView addSubview:bangTypeLabel];
        
        CGSize size = [accountLabel.text sizeWithMaxSize:CGSizeMake(MAXFLOAT, MAXFLOAT) font:15];
        CGFloat margin = (44-size.height)/2;
        [accountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_bgView).offset(10);
            make.top.equalTo(_bgView).offset(margin);
            make.width.mas_equalTo(size.width+2);
        }];
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.width.equalTo(_bgView).offset(10);
            make.top.equalTo(accountLabel.mas_bottom).offset(2*margin);
        }];
        [bangTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_bgView).offset(10);
            make.bottom.equalTo(_bgView).offset(-margin);
        }];
        
        self.cardField = [UITextField new];
        self.nameField = [UITextField new];
        self.bankTypeField = [UITextField new];
        self.cardField.placeholder = @"请输入您的银行卡账号";
        self.nameField.placeholder = @"请输入您的真实姓名";
        self.bankTypeField.placeholder = @"请输入您的银行卡类型 (例如: 工商银行)";
        self.cardField.font = [UIFont systemFontOfSize:14];
        self.nameField.font = [UIFont systemFontOfSize:14];
        self.bankTypeField.font = [UIFont systemFontOfSize:14];
        self.cardField.textColor = GLobalFontColor;
        self.nameField.textColor = GLobalFontColor;
        self.bankTypeField.textColor = GLobalFontColor;
        [_bgView addSubview:self.cardField];
        [_bgView addSubview:self.nameField];
        [_bgView addSubview:self.bankTypeField];
        
        [self.cardField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(accountLabel.mas_right).offset(15);
            make.centerY.equalTo(accountLabel);
            make.right.equalTo(_bgView);
            make.height.mas_equalTo(44);
        }];
        
        [self.nameField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.height.equalTo(self.cardField);
            make.centerY.equalTo(nameLabel);
        }];
        
        [self.bankTypeField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.height.equalTo(self.cardField);
            make.centerY.equalTo(bangTypeLabel);
        }];
        
        UIImageView *lineView1 = [UIImageView lineImageView];
        [_bgView addSubview:lineView1];
        lineView1.frame = CGRectMake(0, 44, SCREEN_WIDTH, 0.5);
        UIImageView *lineView2 = [UIImageView lineImageView];
        [_bgView addSubview:lineView2];
        lineView2.frame = CGRectMake(0, 88, SCREEN_WIDTH, 0.5);
        
        [self bingBtn];
    }
    return _bgView;
}

-(UIButton *)bingBtn
{
    if (_bingBtn == nil)
    {
        _bingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_bingBtn setBackgroundImage:[UIImage imageNamed:@"normal"] forState:UIControlStateNormal];
        [_bingBtn setBackgroundImage:[UIImage imageNamed:@"highLight"] forState:UIControlStateHighlighted];
        [_bingBtn setTitle:@"马上绑定" forState:UIControlStateNormal];
        _bingBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [self.view addSubview:_bingBtn];
        [_bingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.top.equalTo(self.bgView.mas_bottom).offset(75);
            make.left.equalTo(self.view).offset(20);
            make.right.equalTo(self.view).offset(-20);
        }];
        [_bingBtn addTarget:self action:@selector(exchangeEarningsBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *tipLabel = [UILabel new];
        tipLabel.text = @"注: 请仔细检查您的账号和姓名,以保证您的权益";
        tipLabel.textColor = RGB_SAME(153);
        tipLabel.font = [UIFont systemFontOfSize:12];
        [self.view addSubview:tipLabel];
        [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.top.equalTo(_bingBtn.mas_bottom).offset(20);
        }];
    }
    return _bingBtn;
}

-(void)exchangeEarningsBtnDidClick
{
    
    if (!(self.cardField.text.length && self.nameField.text.length && self.bankTypeField.text.length))
    {
        [self showErrorInfoWithStatus:@"请把信息填写完整"];
        return ;
    }
    
    [self showWithStatus:nil];
    
    NSString *url = @"user/bindBankCardAccount.do";
    NSDictionary *dict = @{@"account":self.cardField.text,@"realName":self.nameField.text,@"bankName":self.bankTypeField.text};
    
    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [self dismiss];
        if ([responseObject[@"result"] isEqualToString:@"success"])
        {
            [self showSuccessInfoWithStatus:@"绑定成功"];
            WPAfterBingZFBVC *vc = [WPAfterBingZFBVC new];
            [self.navigationController pushViewController:vc animated:YES];
        }
        else
        {
            if ([responseObject[@"detail"] isEqualToString:@"areadyBind"])
            {
                [self showErrorInfoWithStatus:@"该账号已经绑定"];
            }
            else
            {
                [self showErrorInfoWithStatus:@"服务器错误"];
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self dismiss];
        [self showErrorInfoWithStatus:@"网络错误"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
